home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / PUTW.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-05  |  1.2 KB  |  40 lines

  1. /*  putw.c, from p. 456 of Turbo C Bible  */
  2. #include <stdio.h>
  3.                 /*  The string "Hi There\n" in hexa-     */
  4.                 /*  decimal.  Because of byte-ordering   */
  5.                 /*  conventions they may not look        */
  6.                 /*  obvious.  By the way, here are the   */
  7.                 /*  ASCII codes :                        */
  8.                 /*  H = 48, i = 69, blank = 20, T = 54,  */
  9.                 /*  h = 68, e = 65, and r = 72           */
  10. int words[] = {0x6948, 0x5420, 0x6568, 0x6572, 0x0A0D};
  11. int numw = sizeof(words)/sizeof(int);
  12. main()
  13. {
  14.     int i;
  15.     FILE *infile;
  16.     char filename[81];
  17.     printf("Enter name of a file to write to: ");
  18.     gets(filename);
  19.                 /*  Open the file for reading  */
  20.     if ((infile = fopen(filename, "wb")) == NULL)
  21.     {
  22.     printf("fopen failed.\n");
  23.     exit(0);
  24.     }
  25.                 /*  Write the words to the file  */
  26.     for (i = 0; i < numw; i++)
  27.     {
  28.     if (putw(words[i], infile) == EOF)
  29.     {
  30.                 /*  Check if there was a real error  */
  31.         if (ferror(infile) != 0)
  32.         {
  33.         printf("File: %s write error\n", filename);
  34.         exit(0);
  35.         }
  36.     }
  37.     }
  38.                 /*  Ask user to type file out and check  */
  39.     printf("To see results use 'TYPE %s'\n", filename);
  40. }